﻿using UnityEngine;
using System.Collections;
using System.IO;

public struct stRankingInfo
{
	public string m_strName;
	public int m_nScore;
}

public class CRankingSort : IComparer
{
	public int Compare(object x, object y)
	{
		stRankingInfo xInfo = (stRankingInfo)x;
		stRankingInfo yInfo = (stRankingInfo)y;
		return xInfo.m_nScore.CompareTo(yInfo.m_nScore);
	}
}
public class CRanking
{
	//랭킹 정보를 파일로 기록한다
	public bool WriteRankingFile()
	{
		FileStream fRank;
		fRank = CGlobalMgr.Instance.FileMgr.OpenFile(GlobalDefine.g_strRankingFileName, FileAccess.Write);
		if (null == fRank )
			return false;

		BinaryWriter writer = new BinaryWriter(fRank, System.Text.Encoding.UTF8);
		if (null != writer)
		{
			writer.Write(m_aRankingList.Count);
			for (int nIndex = 0; nIndex < m_aRankingList.Count; nIndex++)
			{
				stRankingInfo _stRankingInfo = (stRankingInfo)m_aRankingList[nIndex];
				writer.Write(_stRankingInfo.m_strName);
				writer.Write(_stRankingInfo.m_nScore);
			}
			writer.Close();
		}
		CGlobalMgr.Instance.FileMgr.CloseFile(fRank);


		return true;
	}
	//랭킹 정보를 파일에서 읽어온다
	public bool ReadRankingFile()
	{
		if (false == CGlobalMgr.Instance.FileMgr.ExistsFile(GlobalDefine.g_strRankingFileName))
			return false;

		FileStream fRank;
		fRank = CGlobalMgr.Instance.FileMgr.OpenFile(GlobalDefine.g_strRankingFileName, FileAccess.Read);

		BinaryReader reader = new BinaryReader(fRank, System.Text.Encoding.UTF8);
		if (null != reader)
		{
			int nListCount = reader.ReadInt32();
			m_aRankingList.Capacity = nListCount;
			for (int nIndex = 0; nIndex < nListCount; nIndex++)
			{
				stRankingInfo _stRankingInfo;
				_stRankingInfo.m_strName = reader.ReadString();
				_stRankingInfo.m_nScore = reader.ReadInt32();
				m_aRankingList.Insert(nIndex, _stRankingInfo);
			}
			reader.Close();
		}
		CGlobalMgr.Instance.FileMgr.CloseFile(fRank);

		_UpdateScore();
		return true;
	}

	public void AddRankingInfo( stRankingInfo _stRankingInfo )
	{
		if( m_nLowScore < _stRankingInfo.m_nScore )
		{
			m_aRankingList.Add(_stRankingInfo);
			_UpdateScore();
			WriteRankingFile();
		}
	}
	//HighScore및 LowScore를 최신 정보로 업데이트 합니다
	private void _UpdateScore()
	{
		if (m_nListMax < m_aRankingList.Count)
		{
			m_aRankingList.RemoveRange(m_nListMax , m_aRankingList.Count-1);
		}

		if (0 != m_aRankingList.Count)
		{
			CRankingSort compare = new CRankingSort();
			m_aRankingList.Sort(compare);

			stRankingInfo stTempRankInfo = (stRankingInfo)m_aRankingList[0];
			m_nHighScore = stTempRankInfo.m_nScore;

			stTempRankInfo = (stRankingInfo)m_aRankingList[m_aRankingList.Count-1];
			m_nLowScore = stTempRankInfo.m_nScore;
		}
	}

	public const int m_nListMax = 10;
	private int m_nHighScore = 0;			//랭킹중 가장 높은점수
	private int m_nLowScore = 0;			//랭킹중 가장 낮은점수
	private ArrayList m_aRankingList = new ArrayList();

	public int nHighScore
	{
		get { return m_nHighScore;  }
	}
}
